﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Threading;
using Clipping.Utils;

namespace ClippingMaster
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            this.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
        }

        void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift) && !Keyboard.IsKeyDown(Key.LeftAlt))
            {
                if (e.Key == Key.S)
                {
                    MainViewModel mainVM = new MainViewModel();
                    if (mainVM != null)
                    {
                        mainVM.SearchEnginesVM.SelectAll = !mainVM.SearchEnginesVM.SelectAll;
                    
                    }
                }
            }
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            MainViewModel mainVM = new MainViewModel();
            this.DataContext = mainVM;
            SettingRepository rep = new SettingRepository();
            var userSetting = rep.GetUserSettings();
            var sysSetting = rep.GetSysSettings();
            if (userSetting.WindowState == WindowState.Maximized.ToString())
                this.WindowState = WindowState.Maximized;
            if (sysSetting.AutoUpdate)
            {
                if (!string.IsNullOrEmpty(sysSetting.ClientInfoUrl) || !string.IsNullOrEmpty(sysSetting.UserInfoUrl) || !string.IsNullOrEmpty(sysSetting.MediaInfoUrl))
                {
                    Settings.InfoFileUpdate updateWin = new Settings.InfoFileUpdate(sysSetting, null);
                    updateWin.ShowDialog();
                }
            }
        }

      

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            MainViewModel vm = this.DataContext as MainViewModel;
            vm.SearchEnginesVM.SaveSelectedSearchEnginesCommand.Execute(null);
            SettingRepository rep = new SettingRepository();
            var userSetting = rep.GetUserSettings();
            userSetting.WindowState = this.WindowState.ToString();
            rep.SaveUserSettings(userSetting);
            base.OnClosing(e);
        }

        private void ContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            ContextMenu cm = sender as ContextMenu;
            var d = cm.DataContext as NewsModel;
            if (d != null && d.HasUnretrivedSameNews)
            {
               var mi= cm.Items.GetItemAt(1) as MenuItem;
               if (mi != null)
                   mi.IsEnabled = true;
            }
            else
            {
                var mi = cm.Items.GetItemAt(1) as MenuItem;
                if (mi != null)
                    mi.IsEnabled = false;
            }
        }

        private void btn_settings_Click(object sender, RoutedEventArgs e)
        {
            Settings.MainWindow win = new Settings.MainWindow();
            win.ShowDialog();

        }

        

       
    }
}
